﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace WzhAddon.DHHPlugIn
{
	internal class HighHeelRunTime : BaseMgr<HighHeelRunTime>
	{
		public Dictionary<int, ShoeFixData> ShoeDataMap = new Dictionary<int, ShoeFixData>();
		private IEnumerator Start()
		{
			yield return 30;
			string path = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/Plugins/DHH";
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
				yield return 1;
			}
			DirectoryInfo directoryInfo = new DirectoryInfo(path);
			FileInfo[] files = directoryInfo.GetFiles("*.dat", SearchOption.TopDirectoryOnly);
			int num;
			for (int i = 0; i < files.Length; i = num + 1)
			{
				yield return 1;
				ShoeFixData shoeFixData = this.LoadShoeData(path, int.Parse(Path.GetFileNameWithoutExtension(files[i].FullName)));
				this.ShoeDataMap.Add(shoeFixData.ID, shoeFixData);
				num = i;
			}
			yield return 1;
			yield break;
		}

		public override void Init() { }
		private ShoeFixData LoadShoeData(string path, int _id)
		{
			ShoeFixData shoeFixData = new ShoeFixData();
			if (!File.Exists(path + "/" + _id.ToString() + ".dat"))
			{
				shoeFixData.ID = _id;
				shoeFixData.FeetBaseRotOffset = 0f;
				shoeFixData.FeetBaseRollOffset = 0f;
				shoeFixData.FeetRotOffset = 0f;
				shoeFixData.ToeRotOffset = 0f;
				shoeFixData.FeetWidth = 1f;
				shoeFixData.ToeWidth = 1f;
				shoeFixData.ToeThickness = 1f;
				shoeFixData.ToeLength = 1f;
				shoeFixData.HeelX = 0f;
				shoeFixData.HeelY = 0f;
				shoeFixData.HeelZ = 0f;
				shoeFixData.TipX = 0f;
				shoeFixData.TipY = 0f;
				shoeFixData.TipZ = 0f;
			}
			else
			{
				string[] data = new StreamReader(path + "/" + _id.ToString() + ".dat").ReadToEnd().Split(new char[] { '\n' });
				shoeFixData.ID = this.GetIntDataFromDat(0, data, _id);
				shoeFixData.FeetBaseRotOffset = this.GetFloatDataFromDat(1, data, 0f);
				shoeFixData.FeetBaseRollOffset = this.GetFloatDataFromDat(2, data, 0f);
				shoeFixData.FeetRotOffset = this.GetFloatDataFromDat(3, data, 0f);
				shoeFixData.ToeRotOffset = this.GetFloatDataFromDat(4, data, 0f);
				shoeFixData.FeetWidth = this.GetFloatDataFromDat(5, data, 1f);
				shoeFixData.ToeWidth = this.GetFloatDataFromDat(6, data, 1f);
				shoeFixData.ToeThickness = this.GetFloatDataFromDat(7, data, 1f);
				shoeFixData.ToeLength = this.GetFloatDataFromDat(8, data, 1f);
				shoeFixData.HeelX = this.GetFloatDataFromDat(9, data, 0f);
				shoeFixData.HeelY = this.GetFloatDataFromDat(10, data, 0f);
				shoeFixData.HeelZ = this.GetFloatDataFromDat(11, data, 0f);
				shoeFixData.TipX = this.GetFloatDataFromDat(12, data, 0f);
				shoeFixData.TipY = this.GetFloatDataFromDat(13, data, 0f);
				shoeFixData.TipZ = this.GetFloatDataFromDat(14, data, 0f);
			}
			return shoeFixData;
		}

		private float GetFloatDataFromDat(int _id, string[] _data, float _default)
		{
			float result;
			if (_data.Length > _id + 1)
			{
				result = float.Parse(_data[_id]);
			}
			else
			{
				result = _default;
			}
			return result;
		}

		private int GetIntDataFromDat(int _id, string[] _data, int _default)
		{
			int result;
			if (_data.Length > _id + 1)
			{
				result = int.Parse(_data[_id]);
			}
			else
			{
				result = _default;
			}
			return result;
		}

		public static float FormatAngle(float _ori, float _add, float _min, float _max)
		{
			float result;
			if (_add != 0f)
			{
				float num = _ori % 360f;
				if (num > 180f)
				{
					num -= 360f;
				}
				if (num < -180f)
				{
					num += 360f;
				}
				float num2 = num + _add - Mathf.Clamp(num + _add, _min, _max);
				result = _ori + _add - num2;
			}
			else
			{
				result = 0f;
			}
			return result;
		}

		
	}
}
